package logic

import (
	"context"
	"encoding/json"
	"furni-manage/cache"
	"furni-manage/furnirpc/furnirpc"
	"furni-manage/furnirpc/internal/svc"
	"furni-manage/model"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type ManageListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewManageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ManageListLogic {
	return &ManageListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ManageListLogic) ManageList(in *furnirpc.ManageListRequest) (*furnirpc.ManageListResponse, error) {
	// todo: add your logic here and delete this line
	var ManageList []*furnirpc.ManageList
	result, _ := cache.Rdb.Get(cache.Ctx, "manageList").Result()
	if result == "" {
		ManageList = model.FindManageList(model.DB, ManageList, in)
		marshal, _ := json.Marshal(&ManageList)
		cache.Rdb.Set(cache.Ctx, "manageList", marshal, time.Hour*1)
	} else {
		json.Unmarshal([]byte(result), &result)
	}

	return &furnirpc.ManageListResponse{
		List: ManageList,
	}, nil
}
